package priority;

// 声明类PCB，代表一个进程控制块
public class PCB {
    private String name;    // 进程的名称
    private PCB next;       // 链表中的下一个PCB，用于组织多个PCB
    private int priority;   // 进程的优先级，数字越大优先级越高
    private int needTime;   // 进程所需的总运行时间
    private String state;   // 进程的当前状态（例如：就绪(R)，运行，等待，终止）
    private int roundTime;  // 周转时间，即从提交到完成的时间
    private int waitingTime;// 等待时间，即从进程变为就绪状态到其首次获得处理器的时间
    private int lastTime;   // 上一次CPU执行时间，用于计算剩余需要时间和调度
    private int cpuTime;    // 进程已使用的CPU时间

    // 构造函数，初始化进程的基本信息
    public PCB(String name, int priority, int needTime) {
        this.name = name;
        this.priority = priority;
        this.needTime = needTime;
        this.state = "R"; // 默认状态为就绪（Ready）
        this.roundTime = 0;
        this.waitingTime = 0;
        this.lastTime = 0;
    }

    // Getters and Setters
    public int getCpuTime() {
        return cpuTime;
    }

    public void setCpuTime(int cpuTime) {
        this.cpuTime = cpuTime;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public PCB getNext() {
        return next;
    }

    public void setNext(PCB next) {
        this.next = next;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public int getNeedTime() {
        return needTime;
    }

    public void setNeedTime(int needTime) {
        this.needTime = needTime;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public int getRoundTime() {
        return roundTime;
    }

    public void setRoundTime(int roundTime) {
        this.roundTime = roundTime;
    }

    public int getWaitingTime() {
        return waitingTime;
    }

    public void setWaitingTime(int waitingTime) {
        this.waitingTime = waitingTime;
    }

    public int getLastTime() {
        return lastTime;
    }

    public void setLastTime(int lastTime) {
        this.lastTime = lastTime;
    }

    // 重写toString方法以提供PCB的字符串表示，便于打印和调试
    @Override
    public String toString() {
        if (next != null) {
            return "PCB1{" +
                    "name='" + name + '\'' +
                    ", next=" + next.getName() +
                    ", priority=" + priority +
                    ", needTime=" + needTime +
                    ", state='" + state + '\'' +
                    ", roundTime=" + roundTime +
                    ", waitingTime=" + waitingTime +
                    ", lastTime=" + lastTime +
                    '}';
        } else {
            return "PCB1{" +
                    "name='" + name + '\'' +
                    ", next=null" +
                    ", priority=" + priority +
                    ", needTime=" + needTime +
                    ", state='" + state + '\'' +
                    ", roundTime=" + roundTime +
                    ", waitingTime=" + waitingTime +
                    ", lastTime=" + lastTime +
                    '}';
        }
    }
}
